package com.cz.video.common.utils;

import org.springframework.boot.convert.DurationStyle;
import org.springframework.util.StringUtils;

import java.time.Duration;

/**
 * {@code @Description} : 缓存帮助类
 * {@code @Author} : 陈智
 * {@code @Date}: 2023/10/28  11:26
 */
public class CacheHelper {

    /**
     * 获取缓存的持续时间 by 缓存名字
     * @param cacheName
     * @return
     */
    public static Duration getDuration(String cacheName){
        String[] array = StringUtils.delimitedListToStringArray(cacheName, "#");
        if (array.length > 1) {
            return DurationStyle.detectAndParse(array[1]);
        }else throw new RuntimeException("无效的缓存名称，无法获取对应的过期时间: "+cacheName);
    }

    /**
     * 获取缓存的真实缓存名字
     * @param cacheName
     * @return
     */
    public static String getRealName(String cacheName){
        String[] array = StringUtils.delimitedListToStringArray(cacheName, "#");
        if (array.length > 0) {
            return array[0];
        }else throw new RuntimeException("无效的缓存名称，无法获取对应的真实缓存名字: "+cacheName);
    }
}
